package com.wfm.leetcode.editor.cn;

import com.wfm.common.TreeNode;

public class PTwo30二叉搜索树中第K小的元素{
//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    int n = 0;
    int res =-1;
    public int kthSmallest(TreeNode root, int k) {
        dfs(root,k);
        return res;
    }
    void dfs(TreeNode root,int k){
        if(root==null||res!=-1) return;

        dfs(root.left,k);
        n++;
        if(k==n){
            res = root.val;
        }
        dfs(root.right,k);
    }
}
//leetcode submit region end(Prohibit modification and deletion)

    public static void main(String[] args) { 
        Solution solution = new PTwo30二叉搜索树中第K小的元素().new Solution();
    }
}